﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.NBL.DBEntities;
using SzCourt.Logic.NBL;
using SzCourt.Logic.Common;
using KMFrame.Core;
using NBearLite;

namespace SzCourt.Logic.BLL
{
    public class Court : SzCourtLogic
    {
        public IEnumerable<pCourtNotice> GetCourtNoticeTop()
        {
            return NBLData.Select(SZCourtColumns.pCourtNotice)
                .Where(SZCourtColumns.pCourtNotice.CourtDay>=DateTime.Today)
                .SetSelectRange(20, 0, SZCourtColumns.pCourtNotice.CourtNoticeID)
                .OrderBy(SZCourtColumns.pCourtNotice.CourtDay.Asc)
                .ToList<pCourtNotice>();
        }

        public PageResult GetCourtNoticePage(CourtNoticeCondition condition)
        {
            PageResult result = new PageResult();

            WhereClip where = SZCourtColumns.pCourtNotice.CourtDay >=DateTime.Today;
            if (condition.CourtDay != null && condition.CourtDay > DateTime.Parse("0001/1/1 0:00:00"))
                where = SZCourtColumns.pCourtNotice.CourtDay >= condition.CourtDay && SZCourtColumns.pCourtNotice.CourtDay <= condition.CourtDay.AddDays(1);
            if (condition.CourtPlace != null && condition.CourtPlace.Length > 0)
                where = where && SZCourtColumns.pCourtNotice.CourtPlace.Like(condition.CourtPlace+"%");
            if (condition.CaseName != null && condition.CaseName.Length > 0)
                where = where && SZCourtColumns.pCourtNotice.CaseName.Contains(condition.CaseName);
            if (condition.KeyWord != null && condition.KeyWord.Length > 0)
                where = where && (SZCourtColumns.pCourtNotice.CauseOfAction.Contains(condition.KeyWord) || SZCourtColumns.pCourtNotice.CaseName.Contains(condition.KeyWord) || SZCourtColumns.pCourtNotice.Plaintiff.Contains(condition.KeyWord) || SZCourtColumns.pCourtNotice.Defendant.Contains(condition.KeyWord));

            result.ResultList = NBLData.Select(SZCourtColumns.pCourtNotice)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.pCourtNotice.CourtNoticeID)
                .Where(where)
                .OrderBy(SZCourtColumns.pCourtNotice.CourtDay.Asc)
                .ToList<pCourtNotice>();
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pCourtNotice, SZCourtColumns.pCourtNotice.CourtNoticeID, where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }
    }

    public class CourtNoticeCondition : PageParam
    {
        /// <summary>
        /// 开庭时间
        /// </summary>
        public DateTime CourtDay { get; set; }
        /// <summary>
        /// 开庭地点
        /// </summary>
        public string CourtPlace { get; set; }
        /// <summary>
        /// 案号
        /// </summary>
        public string CaseName { get; set; }
        /// <summary>
        /// 关键字
        /// </summary>
        public string KeyWord { get; set; }
    }
}
